import {isUndefined} from "../utils/helpers";

Component({
    options: {
        addGlobalClass: true,
    },
    lifetimes: {
        moved() {
            console.log('show', this.getTabBar())
        }
    },
    data: {
        selected: 0,
        color: "#1E222E",
        selectedColor: "#FC4039",
        showBorder: true,
        dots: [ //红点数量，对应菜单列表
            0, 0, 0, 0
        ],
        list: [
            {
                "pagePath": "/pages/index/index",
                "text": "首页",
                "iconPath": "/assets/icon/home_default.png",
                "selectedIconPath": "/assets/icon/home_selected.png"
            },
            {
                "pagePath": "/pages/category/category",
                "text": "分类",
                "iconPath": "/assets/icon/category_default.png",
                "selectedIconPath": "/assets/icon/category_selected.png"
            },
            {
                "pagePath": "/pages/cart/cart",
                "text": "购物车",
                "iconPath": "/assets/icon/cart_default.png",
                "selectedIconPath": "/assets/icon/cart_selected.png"
            },
            {
                "pagePath": "/pages/profile/profile/profile",
                "text": "我的",
                "iconPath": "/assets/icon/profile_default.png",
                "selectedIconPath": "/assets/icon/profile_selected.png"
            }
        ],
    },
    methods: {
        switchTab(e) {
            const data = e.currentTarget.dataset;
            const url = data.path;
            if (isUndefined(url) || data.index === this.data.selected) {
                return
            }
            wx.switchTab({
                url
            })
        },

        setCurrentPage(path) {
            const lists = this.data.list;
            for (let i in lists) {
                i = Number(i)
                if (path === lists[i].pagePath) {
                    this.setData({selected: i})
                    break;
                }
            }
        },

        incDotCounts(index, counts = 1, isInc = true) {
            const c = this.data.dots[index]
            if (isUndefined(index)) {
                return
            }
            if (isInc) {
                return this.setDotCounts(index, counts + c)
            }

            this.setDotCounts(index, c - counts)
        },

        /**
         * 设置红点数量
         * @param index
         * @param counts
         */
        setDotCounts(index, counts = 0) {
            counts = Math.max(0, counts)
            const dots = this.data.dots
            if (isUndefined(dots[index]) || dots[index] === counts) {
                return;
            }
            dots[index] = counts;
            this.setData({
                dots
            })
        }
    }
});
